﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using LinqProviders.Utility;

namespace LinqProviders.LinqToNaver
{
    public class NewsSearchResult : NaverSearchResult
    {
        private static readonly string  DummyUri = "http://";

        /// <summary>
        /// Gets or sets the sort. The sort. date : 날짜순(기본값), sim : 유사도순
        /// </summary>
        /// <value>The sort. date : 날짜순(기본값), sim : 유사도순</value>
        public string Sort { get; set; }


        public string Rss { get; set; }
        public string CnTitle { get; set; }
        public Uri CnLink { get; set; }
        public String CnDescription { get; set; }
        public DateTime LastBuildDate { get; set; }
        //public int Total { get; set; }
        //public int Display { get; set; }
        public int Start { get; set; }
        public string Title { get; set; }
        public Uri OriginalLink { get; set; }
        public Uri Link { get; set; }
        public String Description { get; set; }
        public DateTime PubDate { get; set; }

        /// <summary>
        /// Gets the channel query.
        /// </summary>
        /// <param name="xdoc">The xdoc.</param>
        /// <returns></returns>
        private IEnumerable<NewsSearchResult> Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
                return null;

            XDocument xdoc = XDocument.Parse(input);
            var chan = xdoc.Descendants("channel").FirstOrDefault();
            if (chan == null)
                return null;


            Channel rootChannel = new Channel();
            rootChannel.Title = chan.Element("title")!=null ?  chan.Element("title").Value : string.Empty ;
            rootChannel.Link = ConversionHelpers.ToUri(chan.Element("link") != null ? chan.Element("link").Value : string.Empty);
            rootChannel.Description = chan.Element("description") != null ? chan.Element("description").Value : "";
            rootChannel.Total = int.Parse(chan.Element("total") != null ? chan.Element("total").Value : "0");
            rootChannel.Display = int.Parse(chan.Element("display") != null ? chan.Element("display").Value : "0");
            rootChannel.Start = int.Parse(chan.Element("start") != null ? chan.Element("start").Value : "0");
            rootChannel.LastBuildDate = DateTime.Parse(chan.Element("lastBuildDate") != null ? chan.Element("lastBuildDate").Value : DateTime.Now.ToString());

            return from items in chan.Descendants("item")
                   select new NewsSearchResult
                   {
                       CnTitle = rootChannel.Title,
                       CnLink = rootChannel.Link,
                       CnDescription = rootChannel.Description,
                       LastBuildDate = rootChannel.LastBuildDate,
                       Total = rootChannel.Total,
                       Display = rootChannel.Display,
                       Start = rootChannel.Start,
                       Title = items.Element("title") != null ? items.Element("title").Value : "",
                       OriginalLink = ConversionHelpers.ToUri(items.Element("originallink") != null ? items.Element("originallink").Value : DummyUri),
                       Link = ConversionHelpers.ToUri(items.Element("link") != null ? items.Element("link").Value : DummyUri),
                       Description = items.Element("description") != null ? items.Element("description").Value : string.Empty,
                       PubDate = DateTime.Parse(items.Element("pubdate") != null ? items.Element("pubdate").Value : DateTime.Now.ToString())
                   };
        }

        protected override object Parse(string input, string requestedQuery, out int count, out int totalCount)
        {
            IEnumerable<NewsSearchResult> resultList = new List<NewsSearchResult>();
            count = 0;
            totalCount = 0;

            if (String.IsNullOrEmpty(input) == false)
            {
                resultList = Parse(input);
            }


            totalCount = resultList.FirstOrDefault() != null ? resultList.FirstOrDefault().Total : 0;

            return resultList;
        }
    }

    public class NewsItem
    {
        public string Title { get; set; }
        public Uri OriginalLink { get; set; }
        public Uri Link { get; set; }
        public String Description { get; set; }
        public DateTime PubDate { get; set; }
    }
}
